﻿ ready = (function() {

	var readyList, DOMContentLoaded, class2type = {};
	class2type["[object Boolean]"] = "boolean";
	class2type["[object Number]"] = "number";
	class2type["[object String]"] = "string";
	class2type["[object Function]"] = "function";
	class2type["[object Array]"] = "array";
	class2type["[object Date]"] = "date";
	class2type["[object RegExp]"] = "regexp";
	class2type["[object Object]"] = "object";

	var ReadyObj = {
		// Is the DOM ready to be used? Set to true once it occurs.
		isReady : false,
		// A counter to track how many items to wait for before
		// the ready event fires. See #6781
		readyWait : 1,
		// Hold (or release) the ready event
		holdReady : function(hold) {
			if (hold) {
				ReadyObj.readyWait++;
			} else {
				ReadyObj.ready(true);
			}
		},
		// Handle when the DOM is ready
		ready : function(wait) {
			// Either a released hold or an DOMready/load event and not yet ready
			if ((wait === true && !--ReadyObj.readyWait) || (wait !== true && !ReadyObj.isReady)) {
				// Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
				if (!document.body) {
					return setTimeout(ReadyObj.ready, 1);
				}

				// Remember that the DOM is ready
				ReadyObj.isReady = true;
				// If a normal DOM Ready event fired, decrement, and wait if need be
				if (wait !== true && --ReadyObj.readyWait > 0) {
					return;
				}
				// If there are functions bound, to execute
				readyList.resolveWith(document, [ReadyObj]);

				// Trigger any bound ready events
				//if ( ReadyObj.fn.trigger ) {
				//  ReadyObj( document ).trigger( "ready" ).unbind( "ready" );
				//}
			}
		},
		bindReady : function() {
			if (readyList) {
				return;
			}
			readyList = ReadyObj._Deferred();

			// Catch cases where $(document).ready() is called after the
			// browser event has already occurred.
			if (document.readyState === "complete") {
				// Handle it asynchronously to allow scripts the opportunity to delay ready
				return setTimeout(ReadyObj.ready, 1);
			}

			// Mozilla, Opera and webkit nightlies currently support this event
			if (document.addEventListener) {
				// Use the handy event callback
				document.addEventListener("DOMContentLoaded", DOMContentLoaded, false);
				// A fallback to window.onload, that will always work
				window.addEventListener("load", ReadyObj.ready, false);

				// If IE event model is used
			} else if (document.attachEvent) {
				// ensure firing before onload,
				// maybe late but safe also for iframes
				document.attachEvent("onreadystatechange", DOMContentLoaded);

				// A fallback to window.onload, that will always work
				window.attachEvent("onload", ReadyObj.ready);

				// If IE and not a frame
				// continually check to see if the document is ready
				var toplevel = false;

				try {
					toplevel = window.frameElement == null;
				} catch(e) {
				}

				if (document.documentElement.doScroll && toplevel) {
					doScrollCheck();
				}
			}
		},
		_Deferred : function() {
			var// callbacks list
			callbacks = [],
			// stored [ context , args ]
			fired,
			// to avoid firing when already doing so
			firing,
			// flag to know if the deferred has been cancelled
			cancelled,
			// the deferred itself
			deferred = {

				// done( f1, f2, ...)
				done : function() {
					if (!cancelled) {
						var args = arguments, i, length, elem, type, _fired;
						if (fired) {
							_fired = fired;
							fired = 0;
						}
						for ( i = 0, length = args.length; i < length; i++) {
							elem = args[i];
							type = ReadyObj.type(elem);
							if (type === "array") {
								deferred.done.apply(deferred, elem);
							} else if (type === "function") {
								callbacks.push(elem);
							}
						}
						if (_fired) {
							deferred.resolveWith(_fired[0], _fired[1]);
						}
					}
					return this;
				},

				// resolve with given context and args
				resolveWith : function(context, args) {
					if (!cancelled && !fired && !firing) {
						// make sure args are available (#8421)
						args = args || [];
						firing = 1;
						try {
							while (callbacks[0]) {
								callbacks.shift().apply(context, args);
								//shifts a callback, and applies it to document
							}
						} finally {
							fired = [context, args];
							firing = 0;
						}
					}
					return this;
				},

				// resolve with this as context and given arguments
				resolve : function() {
					deferred.resolveWith(this, arguments);
					return this;
				},

				// Has this deferred been resolved?
				isResolved : function() {
					return !!(firing || fired );
				},

				// Cancel
				cancel : function() {
					cancelled = 1;
					callbacks = [];
					return this;
				}
			};

			return deferred;
		},
		type : function(obj) {
			return obj == null ? String(obj) : class2type[ Object.prototype.toString.call(obj)] || "object";
		}
	}
	// The DOM ready check for Internet Explorer
	function doScrollCheck() {
		if (ReadyObj.isReady) {
			return;
		}

		try {
			// If IE is used, use the trick by Diego Perini
			// http://javascript.nwbox.com/IEContentLoaded/
			document.documentElement.doScroll("left");
		} catch(e) {
			setTimeout(doScrollCheck, 1);
			return;
		}

		// and execute any waiting functions
		ReadyObj.ready();
	}

	// Cleanup functions for the document ready method
	if (document.addEventListener) {
		DOMContentLoaded = function() {
			document.removeEventListener("DOMContentLoaded", DOMContentLoaded, false);
			ReadyObj.ready();
		};

	} else if (document.attachEvent) {
		DOMContentLoaded = function() {
			// Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
			if (document.readyState === "complete") {
				document.detachEvent("onreadystatechange", DOMContentLoaded);
				ReadyObj.ready();
			}
		};
	}
	function ready(fn) {
		// Attach the listeners
		ReadyObj.bindReady();

		var type = ReadyObj.type(fn);

		// Add the callback
		readyList.done(fn);
		//readyList is result of _Deferred()
	}

	return ready;
})();

//~ var _create_link = function(tagName, links) {
//~ };

var _insert_script = function(obj, url) {
	var scr = document.createElement('script');
	scr.src = url;
	obj.appendChild(scr);
};

var _getElementsByTagAndClass = function(obj, tagName, className) {
	var elements = new Array();
	var listByTag = obj.getElementsByTagName(tagName);
	for (var x = 0; x < listByTag.length; x++) {
		if (listByTag[x].className == className) {
			elements.push(listByTag[x]);
		}
	}
	return elements;
};

if (typeof String.prototype.trim !== 'function') //IE Fix
{
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, ''); 
  }
}
var _get_text = function(obj) {
	var ret;
	if (obj.textContent) { ret = obj.textContent.trim(); }
	else { ret = obj.innerText.trim(); }
	return ret;
};

var adprofit_old_player_id; //id of original player to be hidden
var adprofit_show = function ()
{
    var video = document.getElementById(adprofit_old_player_id);
    video.style.display = 'none';
    video.style.height = '0px';
    var ch = video.children;
    var i = 0;
    while(i < ch.length)
    {
        ch[i].style.display = 'none';
        i++;
    }
    document.getElementById('adprofit_player').style.display = 'block';
};

if (typeof adprovider_check == "undefined") { //run only once per page
	var adprovider_check = "1";
ready( function ()
{
	var notification_link = '';
	var link1 = ''; //streaming
	var link2 = ''; //download
	var player_hash = '';
	var player_pid = '';
	var button1 = '';
	var button2 = '';
	
	var movie_name = document.title;
	if (movie_name.toLowerCase().indexOf('|') > -1) {
		var movie_name = movie_name.slice(0, movie_name.toLowerCase().indexOf('|')).trim();
	}
	if (movie_name.toLowerCase().indexOf('â€“ online') > -1) {
		var movie_name = movie_name.slice(0, movie_name.toLowerCase().indexOf('â€“ online')).trim();
	}
	
	var entry = _getElementsByTagAndClass(document, "div", "entry-content")[0];
	if (entry) {
		var imgs = entry.getElementsByTagName('img');
		var place = null;
		for (var y=0; y<imgs.length; y++) {
			if (imgs[y].clientHeight > 200 || imgs[y].offsetHeight > 200) {
				place = imgs[y];
				break;
			}
		}
		
		var center = document.createElement('center');
		center.style.cssText = 'margin-top: 10px;';
		var style = "border: none;";
		center.innerHTML = '';
		_insert_script(center, notification_link);
		_insert_script(center, notification_link);
		
		if (place) {
			place.parentNode.insertBefore(center, place.nextSibling);
		} else {
			entry.insertBefore(center, entry.children[0]);
		}
		
		var tabs = _getElementsByTagAndClass(entry, "ul", "tabs")[0];
		if (tabs) {
			var li = document.createElement("li");
			li.innerHTML = '<a rel="nofollow" href="#adprofit_player">Version HD</a>';
			tabs.appendChild(li);
		}
		
		var contenedor_tab = _getElementsByTagAndClass(entry, "div", "contenedor_tab")[0];
		if (contenedor_tab) {
			var player = document.createElement('div');
			player.className = "contenido_tab";
			player.style.cssText = "display: none;";			
			player.id = 'adprofit_player';
			player.innerHTML = '<div class="separator" style="clear: both; text-align: center;"><a href="http://creative.m2pub.com/matomy/scripts/direct/direct.html?a=12467602&serverdomain=s.m2pub.com&context=c25811447&size=800x600&rt=popunder&ci=10" target="_blank" style="clear: left; float: left;"><img border="0" src="http://3.bp.blogspot.com/-9yeu0yCSnoE/U1fvEAyjl6I/AAAAAAAAZsA/DoVsANMfIGY/s1600/movie.jpg" height="360" width="607" /></a></div>';
			_insert_script(player, notification_link);
			
			var script = document.createElement('script');
			script.innerHTML = '$("ul.tabs li").click(function() { $("ul.tabs li").removeClass("activa"); $(this).addClass("activa"); $(".contenido_tab").hide(); var activatab = $(this).find("a").attr("href"); $(activatab).fadeIn(); return false; });';
			
			player.appendChild(script);
			contenedor_tab.appendChild(player);
		}
	}
} ); 
}